﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows.Media;

// This source code obtained from CodeProject and 
// the URL is http://www.codeproject.com/KB/silverlight/TextOnAPathSilverlight.aspx?msg=2872095.
namespace Xceed.Silverlight.Utils
{
  internal class TransformHelper
  {
    public static Matrix GetTransformValue( Transform transform )
    {
      Matrix matrix = Matrix.Identity;

      if( transform is TransformGroup )
      {
        TransformGroup transformGroup = transform as TransformGroup;

        matrix = transformGroup.Value;
      }
      else if( transform is RotateTransform )
      {
        RotateTransform rotateTransform = transform as RotateTransform;

        double angleRadians = rotateTransform.Angle * Math.PI / 180;
        matrix.M11 = Math.Cos( angleRadians );
        matrix.M12 = Math.Sin( angleRadians );
        matrix.M21 = -matrix.M12;
        matrix.M22 = matrix.M11;
      }
      else if( transform is TranslateTransform )
      {
        TranslateTransform translateTranform = transform as TranslateTransform;
        matrix.OffsetX = translateTranform.X;
        matrix.OffsetY = translateTranform.Y;
      }
      else if( transform is ScaleTransform )
      {
        ScaleTransform scaleTransform = transform as ScaleTransform;
        matrix.M11 = scaleTransform.ScaleX;
        matrix.M12 = 0;
        matrix.M21 = 0;
        matrix.M22 = scaleTransform.ScaleY;
      }
      else if( transform is SkewTransform )
      {
        throw new NotSupportedException("Skew transform not supported.");
      }

      return matrix;
    }

    public static Transform CreateTransformClone( Transform transform )
    {
      if( transform is TranslateTransform )
      {
        TranslateTransform oldTransform = transform as TranslateTransform;
        TranslateTransform newTransform = new TranslateTransform();

        newTransform.X = oldTransform.X;
        newTransform.Y = oldTransform.Y;

        return newTransform;
      }
      else if( transform is RotateTransform )
      {
        RotateTransform oldTransform = transform as RotateTransform;
        RotateTransform newTransform = new RotateTransform();

        newTransform.Angle = oldTransform.Angle;

        return newTransform;
      }
      else if( transform is ScaleTransform )
      {
        ScaleTransform oldTransform = transform as ScaleTransform;
        ScaleTransform newTransform = new ScaleTransform();

        newTransform.ScaleX = oldTransform.ScaleX;
        newTransform.ScaleY = oldTransform.ScaleY;

        return newTransform;
      }
      else if( transform is SkewTransform )
      {
        SkewTransform oldTransform = transform as SkewTransform;
        SkewTransform newTransform = new SkewTransform();

        newTransform.AngleX = oldTransform.AngleX;
        newTransform.AngleY = oldTransform.AngleY;

        return newTransform;
      }
#if SILVERLIGHT
      else if( transform is CompositeTransform )
      {
        CompositeTransform oldTransform = transform as CompositeTransform;
        CompositeTransform newTransform = new CompositeTransform();

        newTransform.CenterX = oldTransform.CenterX;
        newTransform.CenterY = oldTransform.CenterY;
        newTransform.Rotation = oldTransform.Rotation;
        newTransform.ScaleX = oldTransform.ScaleX;
        newTransform.ScaleY = oldTransform.ScaleY;
        newTransform.SkewX = oldTransform.SkewX;
        newTransform.SkewY = oldTransform.SkewY;
        newTransform.TranslateX = oldTransform.TranslateX;
        newTransform.TranslateY = oldTransform.TranslateY;

        return newTransform;
      }
#endif
      else if( transform is TransformGroup )
      {
        TransformGroup oldTransform = transform as TransformGroup;
        TransformGroup newTransform = new TransformGroup();

        foreach( Transform child in oldTransform.Children )
        {
          newTransform.Children.Add( CreateTransformClone( child ) );
        }

        return newTransform;
      }
      else // transform is MatrixTransform
      {
        MatrixTransform oldTransform = transform as MatrixTransform;
        MatrixTransform newTransform = new MatrixTransform();

        newTransform.Matrix = oldTransform.Matrix;

        return newTransform;
      }
    }
  }
}
